//
//  UIView+CLExtension.h
//
//  Created by Charlie on 4/24/14.
//  Copyright (c) 2014. All rights reserved.
//

#import "UIView+CLExtension.h"
//#import <objc/runtime.h>

@implementation UIView (CLExtension)

/// 从nib文件加载
+ (instancetype)loadFromNib
{
    return [self loadFromNib:NSStringFromClass([self class])];
}

+ (instancetype)loadFromNib:(NSString *)nibFile
{
    UINib *nib = [UINib nibWithNibName:nibFile bundle:nil];
    NSArray *views = [nib instantiateWithOwner:nil options:nil];
    for (UIView *v in views) {
        if ([v isKindOfClass:self.class])
            return v;
    }
    
    return nil;
}

///  移除所有子视图
- (void)removeSubviews
{
    for (UIView *v in self.subviews)
        [v removeFromSuperview];
}

/// 取消键盘
- (void)cancelKeyBoard {
    for (UIView *tb in self.subviews) {
        if ([tb isKindOfClass:[UITextField class]] || [tb isKindOfClass:[UITextView class]]) {
            [tb resignFirstResponder];
        } else {
            [tb cancelKeyBoard];
        }
    }
}

@end

@implementation UIView (CLExtension_DESI)

//static const char *assoKeyShadowDiffuse = "_k_ShadowDiffuse";

//+ (void)load {
//    //交换2个方法中的IMP
//    Method method1 = class_getInstanceMethod(self.class, @selector(layoutSublayersOfLayer:));
//    Method method2 = class_getInstanceMethod(self.class, @selector(cl_layoutSublayersOfLayer:));
//    method_exchangeImplementations(method1, method2);
//}
//
//- (void)dealloc {
//    objc_removeAssociatedObjects(self);
//}
//
//- (void)cl_layoutSublayersOfLayer:(CALayer *)layer {
//    [self cl_layoutSublayersOfLayer:layer];
//    if (layer == self.layer) {
//        [self refreshShadowPath];
//    }
//}

- (void)setCornerRadius:(CGFloat)cornerRadius {
    self.layer.cornerRadius = cornerRadius;
    //    [self refreshShadowPath];
}

- (CGFloat)cornerRadius {
    return self.layer.cornerRadius;
}

- (void)setBorderWidth:(CGFloat)borderWidth {
    self.layer.borderWidth = borderWidth;
}

- (CGFloat)borderWidth {
    return self.layer.borderWidth;
}

- (UIColor *)borderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}

- (void)setBorderColor:(UIColor *)borderColor {
    self.layer.borderColor = borderColor.CGColor;
}

- (UIColor *)shadowColor {
    return [UIColor colorWithCGColor:self.layer.shadowColor];
}

- (void)setShadowColor:(UIColor *)shadowColor {
    self.layer.shadowColor = shadowColor.CGColor;
    self.layer.shadowOpacity = 1;
}

- (CGSize)shadowOffset {
    return self.layer.shadowOffset;
}

- (void)setShadowOffset:(CGSize)shadowOffset {
    self.layer.shadowOffset = shadowOffset;
}

- (CGFloat)shadowRadius {
    return self.layer.shadowRadius;
}

- (void)setShadowRadius:(CGFloat)shadowRadius {
    self.layer.shadowRadius = shadowRadius;
}

//- (void)setShadowDiffuse:(CGFloat)shadowDiffuse {
//    objc_setAssociatedObject(self, assoKeyShadowDiffuse, @(shadowDiffuse), OBJC_ASSOCIATION_COPY_NONATOMIC);
//    [self refreshShadowPath];
//}
//
//- (CGFloat)shadowDiffuse {
//    return [objc_getAssociatedObject(self, assoKeyShadowDiffuse) doubleValue];
//}
//
//
////
//
//- (void)refreshShadowPath {
//    if (self.cornerRadius>0 && self.shadowDiffuse>0) {
//        CGFloat shadowSize0 = 0;
//        CGRect shadowSpreadRect0 = CGRectMake(-shadowSize0, -shadowSize0, self.bounds.size.width+shadowSize0*2, self.bounds.size.height+shadowSize0*2);
//        CGFloat shadowSpreadRadius0 =  self.cornerRadius == 0 ? 0 : self.layer.cornerRadius+shadowSize0;
//        UIBezierPath *shadowPath0 = [UIBezierPath bezierPathWithRoundedRect:shadowSpreadRect0 cornerRadius:shadowSpreadRadius0];
//        self.layer.shadowPath = shadowPath0.CGPath;
//    }
//}


@end

